from PIL import Image

def create_grid_image(image_path, output_path):
    # 打开图片
    img = Image.open(image_path)
    # 获取图片的尺寸
    width, height = img.size

    # 计算每个格子的尺寸
    cell_width = width // 3
    cell_height = height // 3

    # 创建新的图片列表
    grid_images = []

    # 切割图片并保存到列表中
    for i in range(3):
        for j in range(3):
            # 计算每个格子的左上角坐标
            left = j * cell_width
            top = i * cell_height
            right = left + cell_width
            bottom = top + cell_height

            # 切割图片
            box = (left, top, right, bottom)
            grid_images.append(img.crop(box))

    # 保存图片到指定路径
    for i, grid_img in enumerate(grid_images):
        grid_img.save(f"{output_path}_grid_{i+1}.jpg")

# 使用示例
create_grid_image('D:\\壁纸\\【手套】2024-08-21 18_09_47.png', '.\\images\\')